const MovieHttp = require("../../utils/MovieHttp")
Page({
  data: {
    banner:[],
    inTheaters:[],
    comingSoon:[],
    top250:[]
  },
  onLoad: async function (options) {
    var bannerRes = await MovieHttp.getBannerMovieHttp();
    // console.log(bannerRes.data.banners);
     var inTheaters = await MovieHttp.getInTheatersMovieHttp();
     
    var Theaters = inTheaters.data.res.slice(0,3)
    var inTheaters = []
    Theaters.forEach(item=>{
      var {raiting,pic,title} = item;
      var star = this.handleStar(raiting);
      inTheaters.push({
        star,
        pic,
        title,
        raiting
      })
    })
    var comingSoon = await MovieHttp.getComingSoonMovieHttp();
    var coming = comingSoon.data.res.slice(0,3)
    var comingSoon = []
    coming.forEach(item=>{
      var {rating,pic,title} = item;
      var star = this.handleStar(rating);
      comingSoon.push({
        star,
        pic,
        title,
        rating
      })
    })
    var top250 = await MovieHttp.getTop250MovieHttp();
    var top = top250.data.res.slice(0,3)
    var top250 = []
    top.forEach(item=>{
      var {rating,pic,title} = item;
      var star = this.handleStar(rating);
      top250.push({
        star,
        pic,
        title,
        rating
      })
    })
    this.setData({
      banner:bannerRes.data.banners,
      inTheaters,
      comingSoon,
      top250,
    })
  },
    handleStar(num){
    var arr = []
    for(var i=0;i<5;i++){
        if(num>=2){
            arr.push(2)
        }else if(num>0){
          arr.push(Number(num.toFixed(1)))
        }else{
            arr.push(0)
        }
        num=num-2
    }

    return arr;
},
})